package com.simen.hellobytes.android.eventbus;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class EventBus {

    private static EventBus eventBus;

    public static EventBus getDefault() {
        if(eventBus==null){
            synchronized (EventBus.class){
                if(eventBus==null){
                    eventBus=new EventBus();
                }
            }
        }
        return eventBus;
    }

    private List<Subscriber> subscriberList;

    private EventBus(){
        subscriberList=new ArrayList<>();
    }

    public void publish(Event event){
        Iterator<Subscriber> iterator=subscriberList.iterator();
        while (iterator.hasNext()){
            iterator.next().onEvent(event);
        }
    }

    public void register(Subscriber subscriber){
        subscriberList.add(subscriber);
    }

    public void unRegister(Subscriber subscriber){
        subscriberList.remove(subscriber);
    }
}
